package com.nhjf.dao.teachers;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.nhjf.dao.BaseDao;
import com.nhjf.model.teachers.CoursesComment;
import com.nhjf.pojo.qo.teachers.CoursesCommentQo;
@Repository
public class CoursesCommentDao extends BaseDao<CoursesComment, CoursesCommentQo>{

	@Override
	protected Criteria buildCriteria(Criteria criteria, CoursesCommentQo qo) {
		// TODO Auto-generated method stub
		criteria.setFetchMode("user", qo.isSeachUser()?FetchMode.JOIN:FetchMode.SELECT);	
		if(qo!=null) {
			if(qo.getCoursesId()!=null) {
				criteria.add(Restrictions.eq("coursesId",qo.getCoursesId()));
			}
			if(qo.getUserId()!=null) {
				criteria.add(Restrictions.eq("user.id",qo.getUserId()));
			}
			if(qo.getCommentTime()!=null) {
				criteria.add(Restrictions.eq("commentTime",qo.getCommentTime()));
			}
			if(qo.getCommentType()!=null) {
				criteria.add(Restrictions.eq("commentType",qo.getCommentType()));
			}
			if(qo.getCommentUserType()!=null) {
				criteria.add(Restrictions.eq("commentUserType",qo.getCommentUserType()));
			}
			if(StringUtils.isNotBlank(qo.getComment())) {
				criteria.add(Restrictions.eq("comment",qo.getComment()));
			}
			 criteria.addOrder(Order.desc("commentTime"));
		}
		return criteria;
	}

	@Override
	protected Class<CoursesComment> getEntityClass() {
		// TODO Auto-generated method stub
		return CoursesComment.class;
	}

}
